// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   BSim.java

package jones;

import java.awt.*;
import java.awt.event.*;
import java.io.PrintStream;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import jones.gui.LightView;
import jones.gui.RigidBodyView;
import jones.gui.RobotView;
import jones.gui.SensorPanel;
import jones.gui.SimulationView;
import jones.gui.SliderDialog;
import jones.gui.WallView;
import jones.rigid.OverlappingRigidBodyException;

// Referenced classes of package jones:
//            Simulation, NewSimulation, CollectionSimulation, GizmoSimulation, 
//            LondonSimulation, CurrentSimulation, Wall, SimObject, 
//            AppletResources, World, Simulator, SimpleRobot, 
//            Light, HockeyPuck

public class BSim extends JApplet implements SimObject {
	private class SimulatorControl extends JPanel {

		private JButton startButton = null;
		private JButton stopButton = null;
		private JButton stepButton = null;
		private JButton resetButton = null;
		private boolean isFirstStep = false;

		public SimulatorControl() {
			startButton = new JButton("Start");
			stopButton = new JButton("Stop");
			stepButton = new JButton("Step");
			resetButton = new JButton("Reset");
			isFirstStep = true;
			add(((java.awt.Component) (startButton)));
			add(((java.awt.Component) (stopButton)));
			add(((java.awt.Component) (stepButton)));
			add(((java.awt.Component) (resetButton)));
			startButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					System.out.println("Saving current simulation . . .");
					savedSimulation = currentSimulation.copySimulation();
					System.out.println("CurrentSimulation saved");
					simulator.start();
					System.out.println("Simulator started");
				}

			});
			stopButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					simulator.stop();
					isFirstStep = true;
				}

			});
			stepButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					if (isFirstStep) {
						System.out.println("Saving current simulation . . .");
						savedSimulation = currentSimulation.copySimulation();
						System.out.println("CurrentSimulation saved");
						isFirstStep = false;
					}
					simulator.step();
				}

			});
			resetButton.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					if (savedSimulation != null) {
						System.out.println("Loading saved simulation . . .");
						loadSimulation(savedSimulation);
						System.out.println("Saved simulation loaded.");
					}
				}

			});
		}
	}

	private class RobotTool extends JToggleButton {

		public RobotTool() {
			super("Robot");
			simulationView
					.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

						public void mouseClicked(MouseEvent e) {
							if (isSelected())
								try {
									final SimpleRobot robot = new SimpleRobot(e.getX(), e.getY(),
											0.0D, world);
									final RobotView robotView = new RobotView(robot, world);
									final SensorPanel sensorPanel = new SensorPanel(robot);
									final String cardString = simulationView.getNewCardString();
									final CardLayout cardLayout = (CardLayout) sensorPanels
											.getLayout();
									robotView
											.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

												public void mousePressed(MouseEvent e) {
													cardLayout
															.show(((java.awt.Container) (sensorPanels)),
																	cardString);
												}

											})));
									robotView
											.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

												public void keyPressed(KeyEvent e) {
													if (e.getKeyCode() == 127 || e.getKeyCode() == 8) {
														System.out.println("Delete key pressed for robot");
														currentSimulation
																.removeObservable(((java.util.Observable) (robot)));
														sensorPanels
																.remove(((java.awt.Component) (sensorPanel)));
														sensorPanels.revalidate();
														sensorPanels.repaint();
														simulationView
																.remove(((java.awt.Component) (robotView)));
														simulationView.revalidate();
														simulationView.repaint();
													}
												}

											})));
									currentSimulation.addSimpleRobot(robot);
									sensorPanels.add(((java.awt.Component) (sensorPanel)),
											((Object) (cardString)));
									simulationView.add(((java.awt.Component) (robotView)));
									robot.addObserver(((java.util.Observer) (robotView)));
									cardLayout.show(((java.awt.Container) (sensorPanels)),
											cardString);
									simulationView.revalidate();
								} catch (OverlappingRigidBodyException ex) {
									JOptionPane.showMessageDialog(((java.awt.Component) (e
											.getComponent())), "Rigid bodies cannot overlap.",
											"OverlappingRigidBodyException", 0);
									ex.printStackTrace();
								}
						}

					})));
		}
	}

	private class LightTool extends JToggleButton {

		public LightTool() {
			super("Light");
			simulationView
					.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

						public void mouseClicked(MouseEvent e) {
							if (isSelected()) {
								final Light light = new Light(e.getX(), e.getY(), 1000D);
								final LightView lightView = new LightView(light);
								lightView
										.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

											public void keyPressed(KeyEvent e) {
												if (e.getKeyCode() == 127 || e.getKeyCode() == 8) {
													System.out.println("Delete key pressed for light");
													currentSimulation
															.removeObservable(((java.util.Observable) (light)));
													simulationView
															.remove(((java.awt.Component) (lightView)));
													simulationView.revalidate();
													simulationView.repaint();
												}
											}

										})));
								light.addObserver(((java.util.Observer) (lightView)));
								currentSimulation.addLight(light);
								simulationView.add(((java.awt.Component) (lightView)));
								simulationView.revalidate();
							}
						}

					})));
		}
	}

	private class HockeyPuckTool extends JToggleButton {

		public HockeyPuckTool() {
			super("Hockey");
			simulationView
					.addMouseListener(((java.awt.event.MouseListener) (new MouseAdapter() {

						public void mouseClicked(MouseEvent e) {
							if (isSelected())
								try {
									final HockeyPuck hockeyPuck = new HockeyPuck(e.getX(), e
											.getY());
									currentSimulation.addHockeyPuck(hockeyPuck);
									final RigidBodyView view = new RigidBodyView(
											((jones.rigid.RigidBody) (hockeyPuck)), world);
									view
											.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

												public void keyPressed(KeyEvent e) {
													if (e.getKeyCode() == 127 || e.getKeyCode() == 8) {
														System.out
																.println("Delete key pressed for hockey puck");
														currentSimulation
																.removeObservable(((java.util.Observable) (hockeyPuck)));
														simulationView
																.remove(((java.awt.Component) (view)));
														simulationView.revalidate();
														simulationView.repaint();
													}
												}

											})));
									hockeyPuck.addObserver(((java.util.Observer) (view)));
									simulationView.add(((java.awt.Component) (view)));
									simulationView.revalidate();
								} catch (OverlappingRigidBodyException ex) {
									JOptionPane.showMessageDialog(((java.awt.Component) e
											.getComponent()), "Rigid bodies cannot overlap.",
											"OverlappingRigidBodyException", 0);
									ex.printStackTrace();
								}
						}

					})));
		}
	}

	private class WallTool extends JToggleButton {

		private int width = 0;

		public WallTool() {
			super("Wall");
			width = 6;
			MouseInputAdapter mouseInputAdapter = ((MouseInputAdapter) (new MouseInputAdapter() {

				private static final int WIDTH = 6;
				private Wall wall = null;
				private WallView wallView = null;

				public void mousePressed(MouseEvent e) {
					if (!isSelected()) {
						return;
					} else {
						wall = new Wall(e.getX(), e.getY(), e.getX(), e.getY(), 6D);
						wallView = new WallView(wall, world);
						wallView
								.addKeyListener(((java.awt.event.KeyListener) (new KeyAdapter() {

									private WallView wallViewToRemove = null;
									private Wall wallToDelete = null;

									public void keyPressed(KeyEvent e) {
										if (e.getKeyCode() == 127 || e.getKeyCode() == 8) {
											System.out.println("Delete key pressed for wall");
											currentSimulation
													.removeObservable(((java.util.Observable) (wallToDelete)));
											simulationView
													.remove(((java.awt.Component) (wallViewToRemove)));
											simulationView.revalidate();
											simulationView.repaint();
										}
									}

									{
										wallViewToRemove = wallView;
										wallToDelete = wall;
									}
								})));
						wall.addObserver(((java.util.Observer) (wallView)));
						simulationView.add(((java.awt.Component) (wallView)));
						simulationView.revalidate();
						return;
					}
				}

				public void mouseDragged(MouseEvent e) {
					if (!isSelected()) {
						return;
					} else {
						wall.setEndPoint(e.getX(), e.getY());
						wall.notifyObservers();
						return;
					}
				}

				public void mouseReleased(MouseEvent e) {
					if (!isSelected())
						return;
					wall.setEndPoint(e.getX(), e.getY());
					wall.notifyObservers();
					try {
						currentSimulation.addWall(wall);
					} catch (OverlappingRigidBodyException ex) {
						JOptionPane.showMessageDialog(((java.awt.Component) (e
								.getComponent())), "Rigid bodies cannot overlap.",
								"OverlappingRigidBodyException", 0);
						ex.printStackTrace();
						simulationView.remove(((java.awt.Component) (wallView)));
						simulationView.revalidate();
						simulationView.repaint();
					}
				}

				{
					wall = null;
					wallView = null;
				}
			}));
			simulationView
					.addMouseListener(((java.awt.event.MouseListener) (mouseInputAdapter)));
			simulationView
					.addMouseMotionListener(((java.awt.event.MouseMotionListener) (mouseInputAdapter)));
		}
	}

	private class WorldToolBar extends JPanel {

		private ButtonGroup buttonGroup = null;
		private JToggleButton tools[] = null;

		public WorldToolBar() {
			buttonGroup = new ButtonGroup();
			tools = (new JToggleButton[] { new LightTool(), new WallTool(),
					new RobotTool(), new HockeyPuckTool() });
			setLayout(((java.awt.LayoutManager) (new BorderLayout())));
			JPanel toolsPanel = new JPanel(((java.awt.LayoutManager) (new GridLayout(
					tools.length, 1))));
			add(((java.awt.Component) (toolsPanel)), "North");
			add(((java.awt.Component) (new JPanel())), "Center");
			for (int i = 0; i < tools.length; i++) {
				buttonGroup.add(((javax.swing.AbstractButton) (tools[i])));
				toolsPanel.add(((java.awt.Component) (tools[i])));
				tools[i].setSelected(true);
			}

		}
	}

	private class SimulationMenuItem extends JMenuItem {

		private Simulation simulation = null;

		public void init() {
			addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					loadSimulation(simulation.createNewSimulation());
				}

			});
		}

		public SimulationMenuItem(Simulation simulation) {
			super(simulation.getName());
			this.simulation = simulation;
			init();
		}
	}

	private class LatencyMenuItem extends JMenuItem {

		private SliderDialog sliderDialog = null;

		public LatencyMenuItem() {
			super("Set latency");
			sliderDialog = new SliderDialog(((java.awt.Frame) (null)),
					"Set latency:", 0, 20);
			addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {
					sliderDialog.setValue(world.getLatency());
					sliderDialog.show();
					if (sliderDialog.isCancelled()) {
						return;
					} else {
						world.setLatency(sliderDialog.getValue());
						return;
					}
				}

			});
		}
	}

	private class FantasyModeMenuItem extends JCheckBoxMenuItem {

		public FantasyModeMenuItem() {
			super("Fantasy Mode");
			addItemListener(new ItemListener() {

				public void itemStateChanged(ItemEvent e) {
					world.setFantasyMode(e.getStateChange() == 1);
				}

			});
		}
	}

	private Simulator simulator = null;
	private World world = null;
	private Simulation simulation = null;
	private CurrentSimulation currentSimulation = null;
	private Simulation savedSimulation = null;
	private SimulationView simulationView = null;
	private SimulatorControl simulatorControl = null;
	private WorldToolBar worldToolBar = null;
	private JPanel sensorPanel = null;
	private JPanel sensorPanels = null;
	private JPanel simulationPanel = null;
	private JPanel contentPane = null;
	private JRadioButtonMenuItem fantasyModeOnMenuItem = null;
	private JRadioButtonMenuItem fantasyModeOffMenuItem = null;
	private Simulation simulations[] = { new NewSimulation(),
			new CollectionSimulation(), new GizmoSimulation(), new LondonSimulation() };

	public BSim() {
		currentSimulation = null;
		savedSimulation = null;
		simulationPanel = new JPanel(
				((java.awt.LayoutManager) (new BorderLayout())));
		contentPane = new JPanel(((java.awt.LayoutManager) (new BorderLayout())));
		fantasyModeOnMenuItem = new JRadioButtonMenuItem("Fantasy mode on");
		fantasyModeOffMenuItem = new JRadioButtonMenuItem("Fantasy mode off");
	}

	public void start() {
		super.start();
		System.out.println("BSim.start()");
	}

	public void stop() {
		super.stop();
		System.out.println("BSim.stop()");
	}

	public void init() {
		System.out.println("BSim v1.0");
		System.out.println("BSim.init()");
		AppletResources.init(((java.applet.Applet) (this)));
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		JMenu simulationsMenu = new JMenu("Simulations");
		menuBar.add(simulationsMenu);
		for (int i = 0; i < simulations.length; i++)
			simulationsMenu
					.add(((JMenuItem) (new SimulationMenuItem(simulations[i]))));

		JMenu optionsMenu = new JMenu("Options");
		menuBar.add(optionsMenu);
		optionsMenu.add(((JMenuItem) (new LatencyMenuItem())));
		fantasyModeOnMenuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				world.setFantasyMode(true);
			}

		});
		fantasyModeOffMenuItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				world.setFantasyMode(false);
			}

		});
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(((javax.swing.AbstractButton) (fantasyModeOnMenuItem)));
		buttonGroup.add(((javax.swing.AbstractButton) (fantasyModeOffMenuItem)));
		optionsMenu.add(((JMenuItem) (fantasyModeOnMenuItem)));
		optionsMenu.add(((JMenuItem) (fantasyModeOffMenuItem)));
		loadSimulation(((Simulation) (new NewSimulation())));
		setContentPane(((java.awt.Container) (contentPane)));
	}

	public void destroy() {
		super.destroy();
		System.out.println("BSim.destroy()");
	}

	private void loadSimulation(Simulation simulation) {
		this.simulation = simulation;
		world = simulation.getWorld();
		if (world.isFantasyMode())
			fantasyModeOnMenuItem.doClick();
		else
			fantasyModeOffMenuItem.doClick();
		if (simulator != null)
			simulator.stop();
		simulator = simulation.getSimulator();
		currentSimulation = new CurrentSimulation(simulation);
		savedSimulation = currentSimulation.copySimulation();
		simulator.removeSimObject(((SimObject) (this)));
		simulator.addSimObject(((SimObject) (this)));
		simulationView = new SimulationView(simulation);
		worldToolBar = new WorldToolBar();
		simulatorControl = new SimulatorControl();
		sensorPanel = new JPanel(((java.awt.LayoutManager) (new BorderLayout())));
		sensorPanels = simulationView.getSensorPanels();
		sensorPanel.removeAll();
		sensorPanel.add(((java.awt.Component) (new JPanel())), "Center");
		sensorPanel.add(((java.awt.Component) (simulationView.getSensorPanels())),
				"North");
		simulationPanel.removeAll();
		simulationPanel.add(((java.awt.Component) (simulationView)), "Center");
		simulationPanel.add(((java.awt.Component) (simulatorControl)), "South");
		contentPane.removeAll();
		contentPane.add(((java.awt.Component) (sensorPanel)), "East");
		contentPane.add(((java.awt.Component) (worldToolBar)), "West");
		contentPane.add(((java.awt.Component) (simulationPanel)), "Center");
		contentPane.revalidate();
		contentPane.repaint();
		addWalls();
	}

	public void update(double time) {
		contentPane.repaint();
	}

	private void addWalls() {
		int WIDTH = 20;
		Dimension size = getSize();
		size.width -= worldToolBar.getPreferredSize().width;
		size.width -= sensorPanel.getPreferredSize().width;
		size.height -= simulatorControl.getPreferredSize().height;
		size.height -= getJMenuBar().getPreferredSize().height;
		Wall northWall = new Wall(0.0D, -10D, size.width, -10D, 20D);
		Wall southWall = new Wall(0.0D, size.height + 10, size.width,
				size.height + 10, 20D);
		Wall eastWall = new Wall(size.width + 10, 0.0D, size.width + 10,
				size.height, 20D);
		Wall westWall = new Wall(-10D, 0.0D, -10D, size.height, 20D);
		RigidBodyView northWallView = new RigidBodyView(
				((jones.rigid.RigidBody) (northWall)), world);
		RigidBodyView southWallView = new RigidBodyView(
				((jones.rigid.RigidBody) (southWall)), world);
		RigidBodyView eastWallView = new RigidBodyView(
				((jones.rigid.RigidBody) (eastWall)), world);
		RigidBodyView westWallView = new RigidBodyView(
				((jones.rigid.RigidBody) (westWall)), world);
		northWallView.setFocusable(false);
		southWallView.setFocusable(false);
		eastWallView.setFocusable(false);
		westWallView.setFocusable(false);
		northWall.addObserver(((java.util.Observer) (northWallView)));
		southWall.addObserver(((java.util.Observer) (southWallView)));
		eastWall.addObserver(((java.util.Observer) (eastWallView)));
		westWall.addObserver(((java.util.Observer) (westWallView)));
		world.addRigidBody(((jones.rigid.RigidBody) (northWall)));
		world.addRigidBody(((jones.rigid.RigidBody) (southWall)));
		world.addRigidBody(((jones.rigid.RigidBody) (eastWall)));
		world.addRigidBody(((jones.rigid.RigidBody) (westWall)));
		simulator.addObservable(((java.util.Observable) (northWall)));
		simulator.addObservable(((java.util.Observable) (southWall)));
		simulator.addObservable(((java.util.Observable) (eastWall)));
		simulator.addObservable(((java.util.Observable) (westWall)));
		simulationView.add(((java.awt.Component) (northWallView)));
		simulationView.add(((java.awt.Component) (southWallView)));
		simulationView.add(((java.awt.Component) (eastWallView)));
		simulationView.add(((java.awt.Component) (westWallView)));
		simulationView.revalidate();
	}

}
